from datetime import datetime

from django.contrib.auth.models import AbstractUser
from django.db import models
from django.db.models import PROTECT


class User(AbstractUser):
    """继承AbstractUser类"""

    mobile = models.CharField(max_length=11, unique=True, verbose_name='手机号')  # 手机号
    email = models.EmailField(max_length=100, null=True, blank=True, verbose_name="邮箱", help_text="邮箱")
    name = models.CharField(max_length=11, verbose_name="昵称")  # 昵称

    class Meta:
        db_table = 'tb_user'
        verbose_name = '用户表'
        verbose_name_plural = verbose_name

    def __str__(self):
        """打印对象时直接打印用户名"""
        return self.username


class VerifyCode(models.Model):
    """
    短信验证码
    """
    code = models.CharField(max_length=10, verbose_name="验证码", help_text="验证码")
    mobile = models.CharField(max_length=11, verbose_name="电话", help_text="手机号")
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间", help_text="创建时间")

    class Meta:
        verbose_name = "短信验证码"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.code


class RewardScore(models.Model):
    """悬赏积分"""

    number = models.IntegerField(verbose_name="积分数量")
    user = models.ForeignKey(User, on_delete=PROTECT, verbose_name='所属用户')

    class Meta:
        verbose_name = "悬赏积分"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.number
